package com.wowudi.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wowudi.entity.vo.DictEeVo;
import com.wowudi.listener.DictListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wowudi.mapper.DictMapper;
import com.wowudi.entity.Dict;
import com.wowudi.service.DictService;
import org.springframework.web.multipart.MultipartFile;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService{

    @Autowired
    private DictMapper dictMapper;
    /**
    * Description: 根据id查询数据字典<br/>
    * date: 2022-3-21<br/>
    * @author 贺同志<br/>
    */
    @Override
    public List<Dict> findById(Integer parentId) {
        // 查询顶级字典
        List<Dict> dictList = dictMapper.selectList(new QueryWrapper<Dict>().eq("parent_id",parentId));
        return dictList;
    }


    /**
    * Description: 查询所有字典信息<br/>
    * date: 2022-3-22<br/>
    * @author 贺同志<br/>
    */
    @Override
    public List<Dict> findDictAll(String name) {
        // 查询顶级字典
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0).notIn("is_deleted",2).like("name",name==null?"":name);
        List<Dict> dictList = dictMapper.selectList(wrapper);
        // 是否有子数据
        dictList.forEach(dict -> {
            List<Dict> children = dictMapper.selectList(new QueryWrapper<Dict>().eq("parent_id", dict.getId()));
            dict.setChildren(children);

        });
        return dictList;
    }


    /**
    * Description: 新增数据字典<br/>
    * date: 2022-3-23<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int insertDict(Dict dict) {
        int i = dictMapper.insert(dict);
        return i;
    }


    /**
    * Description: 修改数据字典<br/>
    * date: 2022-3-23<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int updateDict(Dict dict) {
        int i = dictMapper.updateById(dict);
        return i;
    }


    /**
    * Description: 根据id删除数据字典<br/>
    * date: 2022-3-23<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int deleteDict(Dict dict) {
        int i = dictMapper.deleteById(dict.getId());
        if (dict.getChildren() != null){
            dict.getChildren().forEach(item -> {
                dictMapper.deleteById(item.getId());
            });
        }
        return i;
    }


    /**
    * Description: 导出数据字典<br/>
    * date: 2022-4-4<br/>
    * @author 贺同志<br/>
    */
    @Override
    public void exportDictData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "dict";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
        //查询数据库
        List<Dict> dictList = baseMapper.selectList(null);
        //Dict -- DictEeVo
        List<DictEeVo> dictVoList = new ArrayList<>();
        for(Dict dict:dictList) {
            DictEeVo dictEeVo = new DictEeVo();
            // dictEeVo.setId(dict.getId());
            BeanUtils.copyProperties(dict,dictEeVo);
            dictVoList.add(dictEeVo);
        }
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("dict")
                    .doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
    * Description: 导入数据字典<br/>
    * date: 2022-4-4<br/>
    * @author 贺同志<br/>
    */
    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
